﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;

namespace Silvermoon.UI
{
    /// <summary>
    /// Represents the visual appearance of the control when it is in a specific state.
    /// </summary>
    public class VisualState
    {
        #region classes

        protected class TransitionCollection : List<Transition>
        {
        }

        #endregion

        public static readonly VisualState Enabled = new VisualState { Name = VState.Enabled, Duration = 300 };
        public static readonly VisualState Normal = new VisualState() { Name = VState.Normal, Duration = 500 };

        internal static readonly VisualState Empty = new VisualState();

        private string name;
        private Transition[] transitions;

        public VisualState()
            : base()
        {
        }

        public VisualState(string name, params Transition[] transitions)
            : base()
        {
            this.name = name;
            this.Transitions = transitions;
        }

        public VisualState(string name, int duration)
            : this()
        {
            this.Name = name;
            this.Duration = duration;
        }

        public Transition[] Transitions
        {
            get { return transitions; }
            set
            {
                transitions = value;
                foreach (Transition tl in value)
                {
                    if (tl.Duration < 0) tl.Duration = this.Duration;
                }
            }
        }

        /// <summary>
        /// Gets or sets the name of the state.
        /// </summary>
        public string Name
        {
            get { return name ?? string.Empty; }
            set { name = value; }
        }

        /// <summary>
        /// Gets or sets the duration for all setters if not implicitly specified on a setter.
        /// </summary>
        public int Duration;



        public void Animate(IPropertyObject obj, bool useTransition)
        {
            if (Transitions != null)
            {
                foreach (Transition transition in Transitions)
                {
                    transition.Tracking = TrackMode.Keep;
                    transition.BeginAnimation(obj, useTransition);
                }
            }
        }



        public Transition GetTransition(PropertyAccessor property)
        {
            if (transitions==null) return null;
            foreach (var t in this.transitions)
            {
                if (t.Property.Equals(property)) return t;
            }
            return null;
        }
    }
}
